package com.serilizabled;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerilizableD {

	public static void main(String[] args) throws IOException,
			ClassNotFoundException {
		serialD();

		String pathname = "C:" + File.separator + "zzxx.txt";

		File file = new File(pathname);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		fileOutputStream.write("123".getBytes());
		//
		FileInputStream in = new FileInputStream(file);
		byte[] b = new byte[1024];
		in.read(b);
		System.out.println(new String(b));

	}

	private static void serialD() throws FileNotFoundException, IOException,
			ClassNotFoundException {
		String pathname = "C:" + File.separator + "zzx.txt";

		File file = new File(pathname);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		// 序列化
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				fileOutputStream);

		objectOutputStream.writeObject(new Student(11, "zzz", 1));
		objectOutputStream.flush();
		objectOutputStream.close();
		fileOutputStream.close();
		// // 反序列化
		FileInputStream in = new FileInputStream(file);
		ObjectInputStream inputStream = new ObjectInputStream(in);
		Student student = (Student) inputStream.readObject();
		String name = student.getName();
		int age = student.getAge();
		int sex = student.getSex();
		System.out.println(name);
		System.out.println(sex);
		System.out.println(age);
		in.close();
		inputStream.close();
	}

}
